{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "github地址:[https://github.com/cheesezh/python_design_patterns](https://github.com/cheesezh/python_design_patterns)\n",
    "\n",
    "## 题目\n",
    "\n",
    "用代码模拟以下场景，一个游戏角色有生命力，攻击力，防御力等数据，在打Boss前后的数据值会发生变化，如果玩家挑战Boss失败，我们允许玩家可以将游戏数据恢复到与Boss决斗之前的状态。\n",
    "\n",
    "## 基础版本"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "当前角色状态:\n",
      "体力: 100\n",
      "攻击: 100\n",
      "防御: 100\n",
      "当前角色状态:\n",
      "体力: 0\n",
      "攻击: 0\n",
      "防御: 0\n",
      "当前角色状态:\n",
      "体力: 100\n",
      "攻击: 100\n",
      "防御: 100\n"
     ]
    }
   ],
   "source": [
    "class GameRole():\n",
    "    \n",
    "    def __init__(self):\n",
    "        self.vitality = 0\n",
    "        self.attack = 0\n",
    "        self.defense = 0\n",
    "        \n",
    "    def state_display(self):\n",
    "        print(\"当前角色状态:\")\n",
    "        print(\"体力:\",self.vitality)\n",
    "        print(\"攻击:\",self.attack)\n",
    "        print(\"防御:\",self.defense)\n",
    "        \n",
    "    def get_init_state(self):\n",
    "        \"\"\"\n",
    "        从服务器或者本地磁盘加载初始状态\n",
    "        \"\"\"\n",
    "        self.vitality = 100\n",
    "        self.attack = 100\n",
    "        self.defense = 100\n",
    "        \n",
    "    def fight(self):\n",
    "        \"\"\"\n",
    "        与Boss决斗，数据损耗为0\n",
    "        \"\"\"\n",
    "        self.vitality = 0\n",
    "        self.attack = 0\n",
    "        self.defense = 0\n",
    "        \n",
    "        \n",
    "def main():\n",
    "    mario = GameRole()  \n",
    "    mario.get_init_state()  # 大战Boss前，获取初始角色状态\n",
    "    mario.state_display()\n",
    "    \n",
    "    backup = GameRole()  # 保存游戏进度\n",
    "    backup.vitality = mario.vitality\n",
    "    backup.attack = mario.attack\n",
    "    backup.defense = mario.defense\n",
    "    \n",
    "    mario.fight()  # 大战Boss\n",
    "    mario.state_display()\n",
    "    \n",
    "    mario.vitality = backup.vitality  # 读取进度，重新来过\n",
    "    mario.attack = backup.attack\n",
    "    mario.defense = backup.defense\n",
    "    \n",
    "    mario.state_display()\n",
    "    \n",
    "main()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 点评\n",
    "主要问题在于客户端调用：\n",
    "1. 游戏角色细节暴露给了客户端，客户端需要知道游戏角色的生命力，攻击力，防御力等细节；\n",
    "2. 客户端还要对游戏角色进行备份；\n",
    "3. 如果以后增加“魔法值”或者修改现有数据，都要更改客户端；\n",
    "4. 恢复角色的过程也存在同样问题；\n",
    "\n",
    "## 备忘录模式\n",
    "备忘录模式，在不破坏封装性的前提下，捕获一个对象的内部状态，并在该对象之外保存这个状态。这样以后就可将该对象恢复到原先保存的状态。主要包括三种类：\n",
    "- 发起人(Originator)：负责创建一个备忘录Memento，用以记录当前时刻它的内部状态，并可使用备忘录恢复内部状态。Originator可以根据需要决定Memento存储Originator的哪些内部状态；\n",
    "- 备忘录(Memento)：负责存储Originator对象的内部状态，并可防止Originator以外的其他对象访问备忘录Memento。备忘录有两个接口，Caretaker只能看到备忘录的窄接口，它只能将备忘录传递给其他对象。Originator可以看到一个宽接口，允许它访问返回到先前状态的所有数据；\n",
    "- 管理者(Caretaker)：负责保存好备忘录Memento，不能对备忘录的内容进行操作或检查。\n",
    "\n",
    "\n",
    "在题目的场景中，游戏角色类相当于Originator，使用同样的“游戏角色”实例“backup”来做备忘录，这在当需要保存全部信息时，是可以考虑的，而用clone的方式来实现Memento的状态保存可能是更好的方法，但是这样就相当于对上层应用开放了Originator的全部接口，这对于保存备份有时候并不合适。\n",
    "\n",
    "那么当我们不需要保存全部信息以备使用时，需要怎么办？当我们需要保存的并不是全部信息，而只是部分信息，那么就应该有独立的备忘录类Memento，它只拥有需要保存的信息的属性。\n",
    "\n",
    "## 改进版本——备忘录模式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "当前角色状态:\n",
      "体力: 100\n",
      "攻击: 100\n",
      "防御: 100\n",
      "当前角色状态:\n",
      "体力: 0\n",
      "攻击: 0\n",
      "防御: 0\n",
      "当前角色状态:\n",
      "体力: 100\n",
      "攻击: 100\n",
      "防御: 100\n"
     ]
    }
   ],
   "source": [
    "class GameRole():\n",
    "    \n",
    "    def __init__(self):\n",
    "        self.vitality = 0\n",
    "        self.attack = 0\n",
    "        self.defense = 0\n",
    "        \n",
    "    def state_display(self):\n",
    "        print(\"当前角色状态:\")\n",
    "        print(\"体力:\",self.vitality)\n",
    "        print(\"攻击:\",self.attack)\n",
    "        print(\"防御:\",self.defense)\n",
    "        \n",
    "    def get_init_state(self):\n",
    "        \"\"\"\n",
    "        从服务器或者本地磁盘加载初始状态\n",
    "        \"\"\"\n",
    "        self.vitality = 100\n",
    "        self.attack = 100\n",
    "        self.defense = 100\n",
    "        \n",
    "    def fight(self):\n",
    "        \"\"\"\n",
    "        与Boss决斗，数据损耗为0\n",
    "        \"\"\"\n",
    "        self.vitality = 0\n",
    "        self.attack = 0\n",
    "        self.defense = 0\n",
    "        \n",
    "    def save_state(self):\n",
    "        \"\"\"\n",
    "        新增“保存状态”的方法\n",
    "        \"\"\"\n",
    "        return RoleStateMemento(self.vitality, self.attack, self.defense)\n",
    "    \n",
    "    def recovery_state(self, memento):\n",
    "        \"\"\"\n",
    "        新增“恢复状态”的方法\n",
    "        \"\"\"\n",
    "        self.vitality = memento.vitality\n",
    "        self.attack = memento.attack\n",
    "        self.defense = memento.defense\n",
    "        \n",
    "\n",
    "class RoleStateMemento():\n",
    "    \"\"\"\n",
    "    游戏角色状态存储箱\n",
    "    \"\"\"\n",
    "    def __init__(self, vitality, attack, defense):\n",
    "        self.vitality = vitality\n",
    "        self.attack = attack\n",
    "        self.defense = defense\n",
    "        \n",
    "\n",
    "class RoleStateCaretaker():\n",
    "    \"\"\"\n",
    "    角色状态管理者类\n",
    "    \"\"\"\n",
    "    def __init__(self):\n",
    "        self.memento = None\n",
    "        \n",
    "\n",
    "def main():\n",
    "    mario = GameRole()\n",
    "    mario.get_init_state()\n",
    "    mario.state_display()\n",
    "    \n",
    "    state_admin = RoleStateCaretaker()\n",
    "    state_admin.memento = mario.save_state()  # 游戏角色的保存对客户端是透明的\n",
    "    \n",
    "    mario.fight()\n",
    "    mario.state_display()\n",
    "    \n",
    "    mario.recovery_state(state_admin.memento)  # 游戏角色的恢复对客户端是透明的\n",
    "    mario.state_display()\n",
    "    \n",
    "main()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 点评\n",
    "将需要保存的细节封装在Memento类中，那一天要更改保存的细节也不用影响客户端。\n",
    "\n",
    "Memento模式比较适用于功能复杂的，但需要维护或记录属性历史的类，或者需要保存的属性只是众多属性中的一小部分时，Originator可以根据保存的Memento信息还原到前一状态。\n",
    "\n",
    "命令模式也有类似的撤销作用，如果在某个系统中使用命令模式时，需要实现命令的撤销功能，那么命令模式可以使用备忘录模式来存储可撤销操作的状态。有时候一些对象的内部信息必须保存在对象以外的地方，但是必须要由对象自己读取，这时，使用备忘录可以把复杂的对象内部信息对其他的对象屏蔽起来，从而可以恰当的保持封装的边界。\n",
    "\n",
    "当角色的状态改变的时候，有可能这个状态无效，这时候就可以使用暂时存储起来的备忘录将状态复原。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
